package eu.runelord.programmers.java.faq.jcomponentdigitsonly;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class JComponentsDigitsOnly {

	private static final int HEIGHT = 75;
	private static final int WIDTH = 300;

	public static void main(String[] args) {
		przypadek1();
	}

	private static void przypadek1() {
		JFrame frame = new JFrame("Metoda 1 - nieprawidłowa");
		final JTextField textField = new JTextField();
		JButton button = new JButton("Parsuj do liczby");

		frame.setLayout(new GridLayout(2, 1));
		frame.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				przypadek2();
			}
		});

		frame.setSize(WIDTH, HEIGHT);
		frame.setResizable(false);

		frame.add(textField);
		frame.add(button);

		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String stringToParse = textField.getText();
				Integer parsedNumber = null;
				try {
					parsedNumber = Integer.parseInt(stringToParse);
					JOptionPane.showMessageDialog(null, "Liczba: "
							+ parsedNumber.intValue(), "Wynik parsowania",
							JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception exp) {
					JOptionPane
							.showMessageDialog(null, exp.getStackTrace(),
									"Wynik parsowania",
									JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});

		frame.setVisible(true);
	}

	private static void przypadek2() {
		JFrame frame = new JFrame("Metoda 2 - prawidłowa liczby całkowite");
		final JTextField textField = new JTextField();
		JButton button = new JButton("Parsuj do liczby");

		frame.setLayout(new GridLayout(2, 1));
		frame.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				przypadek3();
			}
		});

		frame.setSize(WIDTH, HEIGHT);
		frame.setResizable(false);

		frame.add(textField);
		frame.add(button);

		textField.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar() == '-'
						&& textField.getText().indexOf('-') > -1) {
					e.consume();
					return;
				}
				if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
					e.consume();
			}
		});

		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String stringToParse = textField.getText();
				Integer parsedNumber = null;
				parsedNumber = Integer.parseInt(stringToParse);
				JOptionPane.showMessageDialog(null, "Liczba: "
						+ parsedNumber.intValue(), "Wynik parsowania",
						JOptionPane.INFORMATION_MESSAGE);

			}
		});

		frame.setVisible(true);
	}

	private static void przypadek3() {
		JFrame frame = new JFrame("Metoda 3 - liczby zmiennoprzecinkowe");
		final JTextField textField = new JTextField();
		JButton button = new JButton("Parsuj do liczby");

		frame.setLayout(new GridLayout(2, 1));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setSize(WIDTH, HEIGHT);
		frame.setResizable(false);

		frame.add(textField);
		frame.add(button);

		textField.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar() == '.'
						&& textField.getText().indexOf('.') > -1) {
					e.consume();
					return;
				}
				if (e.getKeyChar() == '-'
						&& textField.getText().indexOf('-') > -1) {
					e.consume();
					return;
				}
				if (!((e.getKeyChar() >= '0' && e.getKeyChar() <= '9') || e
						.getKeyChar() == '.'))
					e.consume();
			}
		});

		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String stringToParse = textField.getText();
				Double parsedNumber = null;
				parsedNumber = Double.parseDouble(stringToParse);
				JOptionPane.showMessageDialog(null, "Liczba: "
						+ parsedNumber.doubleValue(), "Wynik parsowania",
						JOptionPane.INFORMATION_MESSAGE);

			}
		});

		frame.setVisible(true);
	}
}
